package org.fuys.etp.core.reject;

import org.fuys.etp.core.model.vo.TpBaseConfigVO;
import org.fuys.etp.core.pool.EtpThreadPoolExecutor;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.LongAdder;

/**
 * @projectName: fuys-etp
 * @package: org.fuys.etp.core.reject
 * @className: EtpRejectHandler
 * @author: WangJing
 * @description: 拒绝策略处理器
 * @date: 2025/2/5 16:55
 * @version: 1.0
 */
public abstract class EtpRejectHandler implements RejectedExecutionHandler {

    private LongAdder counter = new LongAdder();

    private TpBaseConfigVO baseConfig;

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        counter.increment();
        if(executor instanceof EtpThreadPoolExecutor){
            this.doRejectHandle(r,executor);
        }
    }

    public long rejectCount(){
        return counter.sum();
    }

    public void removeCount(){
        counter.reset();
    }

    protected abstract void doRejectHandle(Runnable r, ThreadPoolExecutor executor);
}
